﻿using BW.EatRabbit.API.Application.Commands.Farming;
using BW.EatRabbit.Domain;
using BW.EatRabbit.ErrorCode;
using BW.EatRabbit.Infrastructure.Interfaces;
using MediatR;

namespace BW.EatRabbit.API.Application.CommandHandlers.Farming
{
    public class FarmingQueryCommandHandler : IRequestHandler<FarmingQueryCommand, APIResult<Farming_Company>>
    {
        private readonly IBaseRepository<Farming_Company> _farmingRepository;
        public FarmingQueryCommandHandler(IBaseRepository<Farming_Company> farmingRepository)
        {

            _farmingRepository = farmingRepository;
        }
        public Task<APIResult<Farming_Company>> Handle(FarmingQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<Farming_Company> result = new APIResult<Farming_Company>();
            var list = _farmingRepository.GetAll().FirstOrDefault(x => x.ProductionBatch == request.ProductionBatch);
            if (list == null)
            {
                result.Code = ResultCode.查询失败;
                result.Message = "没有该条信息";
                return Task.FromResult(result);
            }

            result.Data = list;
            result.Code = ResultCode.查询成功;
            result.Message = ResultCode.查询成功.ToString();
            return Task.FromResult(result);
        }
    }
}
